package com.itheima.day14.ZuoYe.ZuoYe1;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class Test {
    public static void main(String[] args) {
        try(FileReader fr = new FileReader(new File("D:\\javajc_ws\\javaEE\\src\\com\\itheima\\day14\\ZuoYe\\ZuoYe1\\work.txt"));
            BufferedReader br = new BufferedReader(fr);)
        {

            Map<String, Integer> map = new HashMap<>();
            while (true) {
                String line = br.readLine();
                if(line != null){
                String[] split1 = line.split(",");
                for (int i = 0; i < split1.length; i++) {
                    if(map.containsKey(split1[i])){
                        map.put(split1[i], map.get(split1[i])+1);
                    }else{
                        map.put(split1[i], 1);
                    }
                }
                }else{
                    break;
                }
            }
            map.forEach((k,v)-> System.out.println(k+"---"+v));
            LinkedHashMap<String, Integer> res = map.entrySet().stream().sorted((entry1, entry2) -> Integer.compare(entry2.getValue(), entry1.getValue())).collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue(), (oldValue, newValue) -> oldValue, LinkedHashMap::new));
            res.entrySet().stream().limit(6).forEach(System.out::println);

        }catch (Exception e){
            e.printStackTrace();
        }
    }
}


